package com.is.add.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;


public class HttpUtil {
    /**
     * @param urlString
     * @return
     */
    public static String get(String urlString, String Code) {

        StringBuffer stringBuffer = new StringBuffer();
        if (urlString.equalsIgnoreCase("")) {
            return null;
        } else if (urlString.toLowerCase().startsWith("http://")) {
        } else {
            return null;
        }

        HttpURLConnection httpConnection;
        URL url;
        int code;
        try {
            url = new URL(urlString);
            httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setRequestMethod("GET");
            httpConnection.setDoOutput(true);
            httpConnection.setDoInput(true);
            httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpConnection.setRequestProperty("User-Agent", "Mozilla/4.0");

            code = httpConnection.getResponseCode();
        } catch (Exception e) {
            return null;
        }
        if (code == HttpURLConnection.HTTP_OK) {
            try {
                String strCurrentLine;
                BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), Code));
                while ((strCurrentLine = reader.readLine()) != null) {
                    stringBuffer.append(strCurrentLine).append("\n");
                }
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stringBuffer.toString();
    }


    /**
     * post
     *
     * @param urlString
     * @param parameters
     * @return
     */


    public static String postJson(String urlString, String parameters, String outCode, String inCode) {
        StringBuffer stringBuffer = new StringBuffer();

        if (urlString.equalsIgnoreCase("")) {
            return null;
        } else if (urlString.toLowerCase().startsWith("http://")) {
        } else {
            return null;
        }

        HttpURLConnection httpConnection;
        URL url;
        int code;
        try {
            url = new URL(urlString);
            httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setRequestMethod("POST");
            httpConnection.setRequestProperty("Content-Length", String.valueOf(parameters.length()));
            httpConnection.setRequestProperty("Accept", "application/json");
            httpConnection.setRequestProperty("Content-Type", "application/json");
            httpConnection.setConnectTimeout(30000);
            httpConnection.setReadTimeout(30000);
            httpConnection.setDoOutput(true);
            httpConnection.setDoInput(true);

            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpConnection.getOutputStream(), outCode);
            outputStreamWriter.write(parameters);
            outputStreamWriter.flush();
            outputStreamWriter.close();


            code = httpConnection.getResponseCode();
        } catch (Exception e) {
            return null;
        }
        if (code == HttpURLConnection.HTTP_OK) {
            try {
                String strCurrentLine;
                BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), inCode));
                while ((strCurrentLine = reader.readLine()) != null) {
                    stringBuffer.append(strCurrentLine).append("\n");
                }
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                String strCurrentLine;
                BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), inCode));
                while ((strCurrentLine = reader.readLine()) != null) {
                    stringBuffer.append(strCurrentLine).append("\n");
                }
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stringBuffer.toString();
    }

    public static String postFormData(String urlString, String parameters, String outCode, String inCode) {

        StringBuffer stringBuffer = new StringBuffer();

        if (urlString.equalsIgnoreCase("")) {
            return null;
        } else if (urlString.toLowerCase().startsWith("http://")) {
        } else {
            return null;
        }

        HttpURLConnection httpConnection;
        URL url;
        int code;
        try {
            url = new URL(urlString);
            httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setRequestMethod("POST");
            httpConnection.setRequestProperty("Content-Length", String.valueOf(parameters.length()));
            httpConnection.setRequestProperty("Content-Type", "multipart/form-data");
            httpConnection.setDoOutput(true);
            httpConnection.setDoInput(true);

            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpConnection.getOutputStream(), outCode);
            outputStreamWriter.write(parameters);
            outputStreamWriter.flush();
            outputStreamWriter.close();


            code = httpConnection.getResponseCode();
        } catch (Exception e) {
            return null;
        }
        if (code == HttpURLConnection.HTTP_OK) {
            try {
                String strCurrentLine;
                BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), inCode));
                while ((strCurrentLine = reader.readLine()) != null) {
                    stringBuffer.append(strCurrentLine).append("\n");
                }
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stringBuffer.toString();
    }


    public static String postFormUrlEncoded(String urlString, String parameters, String outCode, String inCode) {
        StringBuffer stringBuffer = new StringBuffer();
        if (urlString.equalsIgnoreCase("")) {
            return null;
        } else if (urlString.toLowerCase().startsWith("http://")) {
        } else {
            return null;
        }

        HttpURLConnection httpConnection;
        URL url;
        int code;
        try {
            url = new URL(urlString);
            httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setRequestMethod("POST");
            httpConnection.setRequestProperty("Content-Length", String.valueOf(parameters.length()));
            httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            httpConnection.setDoOutput(true);
            httpConnection.setDoInput(true);

            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpConnection.getOutputStream(), outCode);
            outputStreamWriter.write(parameters);
            outputStreamWriter.flush();
            outputStreamWriter.close();


            code = httpConnection.getResponseCode();
        } catch (Exception e) {
            return null;
        }
        if (code == HttpURLConnection.HTTP_OK) {
            try {
                String strCurrentLine;
                BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), inCode));
                while ((strCurrentLine = reader.readLine()) != null) {
                    stringBuffer.append(strCurrentLine).append("\n");
                }
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        return stringBuffer.toString();
    }


    /*public static void main(String[] args) {
        try {

            StringBuffer stringBuffer = new StringBuffer();
            //String parameters= "opt=load&hdr=UserCard&_json={\"pageInfo\":{\"size\":10,\"row\":1},\"filterInfo\":[{\"field\":\"date\",\"logic\":\"=\",\"value\":\"8\"}],\"sortInfo\":[{\"field\":\"id\",\"sort\":\"ASC\"}]}";
            //String parameters= "opt=load&hdr=UserCard&_json={\"filterInfo\":[{\"field\":\"userId\",\"logic\":\"=\",\"value\":\"234\"},{\"field\":\"shop\",\"logic\":\"=\",\"value\":\"2070\"},{\"field\":\"status\",\"logic\":\"=\",\"value\":\"1\"}],\"sortInfo\":[{\"field\":\"timeFrom,stadium\",\"sort\":\"ASC\"}],\"user\":{\"id\":\"234\",\"pwd\":\"9d877ee015f5fe26788525558fe0d809\"}}}";
            //URL url = new URL("http://service.dongqil.com/iSports/api");
            BimensionCode b = new BimensionCode();
            AydUser user = new AydUser();
            user.setId(1);
            b.setUserId(user);  //用户id
            Shop shop = new Shop();
            shop.setId(3);  //商户id
            b.setShop(shop);
            b.setAuthCode("13131"); //验证码
            b.setEffectiveDate(new Timestamp(2015 - 7 - 10));  //有效期
            b.setGenerTime(new Timestamp(new java.util.Date().getTime())); //生成时间
            b.setStatus(1); //状态 1 使用 2， 未使用 ，3 过期
            //JSONObject object = JSONObject.fromObject(b);
            //String parameters= "opt=load&hdr=BimensionCode&_json={\"filterInfo\":[{\"field\":\"id\",\"logic\":\"=\",\"value\":\"1\"}]}";
            String parameters = "opt=add&hdr=BimensionCode&_json={\"data\":[{\"authCode\":\"34343\"}]}";
            URL url = new URL("http://service.dongqil.com/iSports/api");
            HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setRequestMethod("POST");
            httpConnection.setRequestProperty("Content-Length", String.valueOf(parameters.length()));
            httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpConnection.setDoOutput(true);
            httpConnection.setDoInput(true);

            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpConnection.getOutputStream(), "utf-8");
            outputStreamWriter.write(parameters);
            outputStreamWriter.flush();
            outputStreamWriter.close();


            int code = httpConnection.getResponseCode();


            if (code == HttpURLConnection.HTTP_OK) {

                String strCurrentLine;
                BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), "utf-8"));
                while ((strCurrentLine = reader.readLine()) != null) {
                    stringBuffer.append(strCurrentLine).append("\n");
                }
                reader.close();

            }
            System.out.println(stringBuffer.toString());
        } catch (IOException e) {
            e.printStackTrace();

        }

    }*/
}
